package com.javalong.affairs.module.renwujinzhang.activity.progress;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.javalong.affairs.R;
import com.javalong.affairs.base.common.ViewHolder;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.listener.DefaultResponseListener;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.TimeUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.javalong.affairs.module.renwujinzhang.activity.RecycleListActivity;
import com.javalong.affairs.module.renwujinzhang.activity.WatchRunTaskActivity;
import com.javalong.affairs.module.renwujinzhang.model.ProgressModel;
import com.javalong.affairs.module.renwujinzhang.model.Task;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 阶段界面
 */
public class UserListActivity extends RecycleListActivity {

    private List<ProgressModel> progressModels;

    public static void toThisActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, UserListActivity.class);
        context.startActivity(intent);
    }

    public static void toThisActivity(Context context, List<ProgressModel> progressModels) {
        Intent intent = new Intent();
        intent.setClass(context, UserListActivity.class);
        intent.putExtra(BundleKey.PROGRESSLIST, (Serializable) progressModels);
        context.startActivity(intent);
    }

    @Override
    public void afterInit() {
        getSupportActionBar().setTitle("任务进度列表");
        progressModels = (List<ProgressModel>) getIntent().getSerializableExtra(BundleKey.PROGRESSLIST);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.item_user, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final ProgressModel progressModel = progressModels.get(position);
        if (progressModel == null) {
            return;
        }
        TextView tv_user = holder.getView(R.id.tv_user);
        tv_user.setText(progressModel.getReceiveName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, Object> param = new HashMap<String, Object>();
                param.put(RequestKey.ID, progressModel.getId());
                String url = UrlUtils.getUrl("Task", "outOne_detail");
                HttpUtils.post(url, param, new BaseResponseListener(new TypeToken<Task>() {
                }.getType()) {
                    @Override
                    public void onSuccess(Object obj) {
                        WatchRunTaskActivity.toThisActivity(UserListActivity.this, (Task) obj);
                    }
                });
            }
        });
    }

    @Override
    public int getItemCount() {
        return progressModels.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }
}
